﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Windows.Forms;
using Assist;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.ExtensibleStorage;
using Autodesk.Revit.UI;
using YArchitech.ProgressBar;
using YArchitech.Revit;
using YArchitech.Revit.RevitExtension;
using HYView;

namespace YJKArch.AxisModule
{
	[Regeneration(RegenerationOption.Manual)]
	[Transaction(TransactionMode.Manual)]
	internal class CmdGridToModelCurve : YJKCommandManual
	{
		protected override Result YJKExcute(ExternalCommandData cmdData, ref string message, ElementSet elements)
		{
			CmdViewType types = (CmdViewType)3;
			if (SwitchView.SetUsableView(types, cmdData) != Autodesk.Revit.UI.Result.Succeeded)
			{
				return Autodesk.Revit.UI.Result.Failed;
			}
			Autodesk.Revit.DB.Document document = cmdData.Application.ActiveUIDocument.Document;
			try
			{
				FilteredElementCollector filteredElementCollector = new FilteredElementCollector(this.m_doc).OfCategory(BuiltInCategory.OST_GridChains).OfClass(typeof(MultiSegmentGrid));
				List<ElementId> list = filteredElementCollector.ToElementIds().ToList<ElementId>();
				FilteredElementCollector filteredElementCollector2 = new FilteredElementCollector(this.m_doc).OfCategory(BuiltInCategory.OST_Grids).OfClass(typeof(Grid));
				if (filteredElementCollector.Count<Element>() == 0 && filteredElementCollector2.Count<Element>() == 0)
				{
					MessageBox.Show("当前项目中没有轴网。", "盈建科提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
					return Autodesk.Revit.UI.Result.Succeeded;
				}
				DlgViewSelect dlgViewSelect = new DlgViewSelect(CmdGridToModelCurve.GetAllView(this.m_doc), CmdGridToModelCurve.GetAllLineStyle(this.m_doc), CmdGridToModelCurve.GetAllTextStyle(this.m_doc), CmdGridToModelCurve.GetAllLevel(this.m_doc), this.m_doc);
				DialogResult dialogResult = dlgViewSelect.ShowDialog();
				if (dialogResult == DialogResult.OK)
				{
					List<ViewOrientation3D> list2 = new List<ViewOrientation3D>();
					foreach (Autodesk.Revit.DB.View view in dlgViewSelect.ViewList)
					{
						ViewOrientation3D orientation = (view as Autodesk.Revit.DB.View3D).GetOrientation();
						list2.Add(orientation);
					}
					Transaction transaction = new Transaction(this.m_doc, "GridToModelCurve");
					try
					{
						transaction.Start();
						FilteredElementCollector filteredElementCollector3 = new FilteredElementCollector(this.m_doc).OfCategory(BuiltInCategory.OST_Lines).OfClass(typeof(CurveElement));
						FilteredElementCollector filteredElementCollector4 = new FilteredElementCollector(this.m_doc).OfCategory(BuiltInCategory.OST_TextNotes).OfClass(typeof(TextNote));
						ICollection<ElementId> collection = new List<ElementId>();
						foreach (ElementId elementId in filteredElementCollector3.ToElementIds())
						{
							if (CmdGridToModelCurve.ReadData(this.m_doc.GetElement(elementId)) == "Grid")
							{
								collection.Add(elementId);
							}
						}
						foreach (ElementId elementId2 in filteredElementCollector4.ToElementIds())
						{
							if (CmdGridToModelCurve.ReadData(this.m_doc.GetElement(elementId2)) == "Grid")
							{
								collection.Add(elementId2);
							}
						}
						this.m_doc.Delete(collection);
						ProgressBarForm progressBarForm = null;
						WindowHandle owner = new WindowHandle(Process.GetCurrentProcess().MainWindowHandle);
						int num = 0;
						num += filteredElementCollector2.ToElementIds().Count;
						num = dlgViewSelect.ViewList.Count * dlgViewSelect.LevelList.Count * num;
						if (num > 0)
						{
							progressBarForm = new ProgressBarForm(num, ProgressBarStyle.Blocks, true);
						}
						if (progressBarForm != null)
						{
							progressBarForm.Show(owner);
						}
						foreach (ElementId elementId3 in filteredElementCollector.ToElementIds())
						{
							MultiSegmentGrid grid = this.m_doc.GetElement(elementId3) as MultiSegmentGrid;
							if (!this.DrawModelCurve(grid, dlgViewSelect.ViewList, dlgViewSelect.Style, dlgViewSelect.TextStyle, dlgViewSelect.LevelList, progressBarForm))
							{
								if (transaction.HasStarted())
								{
									transaction.RollBack();
								}
								return Autodesk.Revit.UI.Result.Succeeded;
							}
						}
						foreach (ElementId elementId4 in filteredElementCollector2.ToElementIds())
						{
							bool flag = false;
							foreach (ElementId elementId5 in list)
							{
								if ((this.m_doc.GetElement(elementId5) as MultiSegmentGrid).GetGridIds().Contains(elementId4))
								{
									flag = true;
								}
							}
							if (!flag)
							{
								Grid grid2 = this.m_doc.GetElement(elementId4) as Grid;
								if (!this.DrawModelCurve(grid2, dlgViewSelect.ViewList, dlgViewSelect.Style, dlgViewSelect.TextStyle, dlgViewSelect.LevelList, progressBarForm))
								{
									if (transaction.HasStarted())
									{
										transaction.RollBack();
									}
									return Autodesk.Revit.UI.Result.Succeeded;
								}
							}
						}
						if (progressBarForm != null)
						{
							progressBarForm.Dispose();
						}
						transaction.Commit();
					}
					catch (Exception)
					{
						transaction.RollBack();
					}
					int num2 = 0;
					foreach (Autodesk.Revit.DB.View view2 in dlgViewSelect.ViewList)
					{
						(view2 as Autodesk.Revit.DB.View3D).SetOrientation(list2[num2]);
						num2++;
					}
					this.m_uiDoc.ActiveView = dlgViewSelect.ViewList[0];
				}
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.Message, "盈建科提示");
			}
			return Autodesk.Revit.UI.Result.Succeeded;
		}

		private bool DrawModelCurve(MultiSegmentGrid grid, List<Autodesk.Revit.DB.View> viewList, GraphicsStyle style, TextElementType type, List<Level> levels, ProgressBarForm tempUsePBar)
		{
			bool result = true;
			foreach (Level level in levels)
			{
				double num = 0.8 * AssistFunc.feetToMM(type.get_Parameter(BuiltInParameter.TEXT_SIZE).AsDouble()) / 2.0;
				if (num < 1.45)
				{
					num = 1.45;
				}
				foreach (Autodesk.Revit.DB.View view in viewList)
				{
					View3D view3D = view as Autodesk.Revit.DB.View3D;
					view3D.GetOrientation();
					ViewOrientation3D orientation = new ViewOrientation3D(new XYZ(0.0, 0.0, 100.0), XYZ.BasisY, -XYZ.BasisZ);
					view3D.SetOrientation(orientation);
					Category category = this.m_doc.Settings.Categories.get_Item(BuiltInCategory.OST_TextNotes);
					using (SubTransaction subTransaction = new SubTransaction(this.m_doc))
					{
						try
						{
							subTransaction.Start();
							view.SetYJKVisibility(category, true);
							subTransaction.Commit();
						}
						catch
						{
							subTransaction.RollBack();
						}
					}
					List<ElementId> list = new List<ElementId>();
					Parameter parameter = null;
					Parameter parameter2 = null;
					Grid grid2 = null;
					Grid grid3 = null;
					for (int i = 0; i < grid.GetGridIds().Count; i++)
					{
						ElementId elementId = grid.GetGridIds().ElementAt(i);
						Grid grid4 = this.m_doc.GetElement(elementId) as Grid;
						parameter = grid4.GetGridType().get_Parameter(BuiltInParameter.GRID_BUBBLE_END_1);
						parameter2 = grid4.GetGridType().get_Parameter(BuiltInParameter.GRID_BUBBLE_END_2);
						ModelCurve modelCurve = this.CreateCurve(grid4.Curve, level.Elevation, this.m_uiApp);
						modelCurve.LineStyle = style;
						CmdGridToModelCurve.WriteData(modelCurve, this.m_doc);
						list.Add(modelCurve.Id);
						if (i == grid.GetGridIds().Count - 1)
						{
							grid3 = grid4;
						}
						if (i == 0)
						{
							grid2 = grid4;
						}
						if (tempUsePBar != null)
						{
							if (tempUsePBar.IsDisposed && tempUsePBar.m_stopTag)
							{
								result = false;
								return result;
							}
							tempUsePBar.RefreshProgressBar();
							if (tempUsePBar.IsDisposed && tempUsePBar.m_stopTag)
							{
								result = false;
								return result;
							}
						}
					}
					if (parameter.AsInteger() == 1)
					{
						XYZ xyz = grid2.Curve.ComputeDerivatives(1.0, true).get_Basis(0).Normalize();
						XYZ xyz2 = grid2.Curve.GetEndPoint(0) + xyz * num;
                        //grid2.Curve.GetEndPoint(1) + xyz * num;
						if (grid2.Curve is Line)
						{
							xyz2 = grid2.Curve.GetEndPoint(1) + xyz * num;
                            //grid2.Curve.GetEndPoint(0) + xyz * num;
						}
						Arc arc = this.m_doc.CreatYJKArc(xyz2, num, 0.0, 6.2631853071795867, XYZ.BasisX, XYZ.BasisY);
						ModelCurve modelCurve2 = this.CreateArc(arc, level.Elevation, this.m_uiApp);
						modelCurve2.LineStyle = style;
                        TextNote textNode = this.m_doc.GetTextNode(view, new XYZ(xyz2.X, xyz2.Y + 0.5, level.Elevation), XYZ.BasisX, XYZ.BasisY, 1.0, (TextAlignFlags)128, grid.Name);
						RevitElementExtension.SetTextNoteWidth(textNode, 1.0);
						textNode.ChangeTypeId(type.Id);
						CmdGridToModelCurve.WriteData(textNode, this.m_doc);
						CmdGridToModelCurve.WriteData(modelCurve2, this.m_doc);
						list.Add(modelCurve2.Id);
						list.Add(textNode.Id);
					}
					if (parameter2.AsInteger() == 1)
					{
						XYZ xyz3 = grid3.Curve.ComputeDerivatives(1.0, true).get_Basis(0).Normalize();
                        //grid3.Curve.GetEndPoint(0) + xyz3 * num;
						XYZ xyz4 = grid3.Curve.GetEndPoint(1) + xyz3 * num;
						if (grid3.Curve is Line)
						{
                            //grid3.Curve.GetEndPoint(1) + xyz3 * num;
							xyz4 = grid3.Curve.GetEndPoint(0) - xyz3 * num;
						}
						Arc cur = this.m_doc.CreatYJKArc(xyz4, num, 0.0, 6.2631853071795867, XYZ.BasisX, XYZ.BasisY);
						ModelCurve modelCurve3 = this.CreateCurve(cur, level.Elevation, this.m_uiApp);
						modelCurve3.LineStyle = style;
                        TextNote textNode2 = this.m_doc.GetTextNode(view, new XYZ(xyz4.X, xyz4.Y + 0.5, level.Elevation), XYZ.BasisX, XYZ.BasisY, 1.0, (TextAlignFlags)128, grid.Name);
						RevitElementExtension.SetTextNoteWidth(textNode2, 1.0);
						textNode2.ChangeTypeId(type.Id);
						CmdGridToModelCurve.WriteData(textNode2, this.m_doc);
						CmdGridToModelCurve.WriteData(modelCurve3, this.m_doc);
						list.Add(modelCurve3.Id);
						list.Add(textNode2.Id);
					}
					this.HideGrid(list, viewList);
				}
			}
			return result;
		}

		private ModelCurve CreateCurve(Curve cur, double height, UIApplication uiApp)
		{
			ModelCurve result = null;
			if (cur is Line)
			{
				result = this.CreateLine(cur as Line, height, uiApp);
			}
			else if (cur is Arc)
			{
				result = this.CreateArc(cur as Arc, height, uiApp);
			}
			return result;
		}

		private ModelCurve CreateLine(Line line, double height, UIApplication uiApp)
		{
			Plane plane = RevitVersionFuncs.CreatePlanByNormalAndOrigin(XYZ.BasisZ, new XYZ(0.0, 0.0, height));
			SketchPlane sketchPlane = RevitVersionFuncs.NewSketchPlane(uiApp.ActiveUIDocument.Document, plane);
			XYZ startPoint = new XYZ(line.GetEndPoint(0).X, line.GetEndPoint(0).Y, height);
			XYZ endPoint = new XYZ(line.GetEndPoint(1).X, line.GetEndPoint(1).Y, height);
			Line line2 = YJKLineEx.YJKGetBound(startPoint, endPoint);
			return uiApp.ActiveUIDocument.Document.Create.NewModelCurve(line2, sketchPlane);
		}

		public static XYZ TransformPoint(XYZ point, Transform transform)
		{
			double x = point.X;
			double y = point.Y;
			double z = point.Z;
			XYZ xyz = transform.get_Basis(0);
			XYZ xyz2 = transform.get_Basis(1);
			XYZ xyz3 = transform.get_Basis(2);
			XYZ origin = transform.Origin;
			double num = x * xyz.X + y * xyz2.X + z * xyz3.X + origin.X;
			double num2 = x * xyz.Y + y * xyz2.Y + z * xyz3.Y + origin.Y;
			double num3 = x * xyz.Z + y * xyz2.Z + z * xyz3.Z + origin.Z;
			return new XYZ(num, num2, num3);
		}

		private ModelCurve CreateArc(Arc arc, double height, UIApplication uiApp)
		{
			ModelCurve result;
			try
			{
				XYZ hyendPoint = arc.GetEndPoint(0);
				XYZ hyendPoint2 = arc.GetEndPoint(1);
				XYZ center = arc.Center;
				new XYZ(-1.0, 0.0, center.Z);
				XYZ basisZ = XYZ.BasisZ;
				XYZ origin = new XYZ(arc.Center.X, arc.Center.Y, height);
				Plane plane = RevitVersionFuncs.CreatePlanByNormalAndOrigin(XYZ.BasisZ, origin);
				SketchPlane sketchPlane = RevitVersionFuncs.NewSketchPlane(uiApp.ActiveUIDocument.Document, plane);
				double radius = arc.Radius;
				if (GeoUtil.IsPointEqual(hyendPoint, hyendPoint2))
				{
					Arc arc2 = this.m_doc.CreatYJKArc(plane, radius, 0.0, Math.PI*2);
					result = uiApp.ActiveUIDocument.Document.Create.NewModelCurve(arc2, sketchPlane);
				}
				else
				{
					XYZ xyz = new XYZ(hyendPoint.X, hyendPoint.Y, height);
					XYZ xyz2 = new XYZ(hyendPoint2.X, hyendPoint2.Y, height);
					XYZ xyz3 = arc.Tessellate()[5];
					XYZ xyz4 = new XYZ(xyz3.X, xyz3.Y, height);
					Arc arc3 = Arc.Create(xyz, xyz2, xyz4);
					result = uiApp.ActiveUIDocument.Document.Create.NewModelCurve(arc3, sketchPlane);
				}
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.Message, "盈建科提示");
				result = null;
			}
			return result;
		}

		private double formatAngle(double angle)
		{
			int num = (int)(angle / 6.283);
			return angle - (double)num * 6.283;
		}

		private bool DrawModelCurve(Grid grid, List<Autodesk.Revit.DB.View> viewList, GraphicsStyle style, TextElementType type, List<Level> levels, ProgressBarForm tempUsePBar)
		{
			bool result = true;
			foreach (Level level in levels)
			{
				double num = 0.8 * AssistFunc.feetToMM(type.get_Parameter(BuiltInParameter.TEXT_SIZE).AsDouble()) / 2.0;
				if (num < 1.45)
				{
					num = 1.45;
				}
				foreach (Autodesk.Revit.DB.View view in viewList)
				{
					View3D view3D = view as Autodesk.Revit.DB.View3D;
					view3D.GetOrientation();
					ViewOrientation3D orientation = new ViewOrientation3D(new XYZ(0.0, 0.0, 100.0), XYZ.BasisY, -XYZ.BasisZ);
					view3D.SetOrientation(orientation);
					Category category = this.m_doc.Settings.Categories.get_Item(BuiltInCategory.OST_TextNotes);
					using (SubTransaction subTransaction = new SubTransaction(this.m_doc))
					{
						try
						{
							subTransaction.Start();
							view.SetYJKVisibility(category, true);
							subTransaction.Commit();
						}
						catch
						{
							subTransaction.RollBack();
						}
					}
					List<ElementId> list = new List<ElementId>();
					Parameter parameter = grid.GetGridType().get_Parameter(BuiltInParameter.GRID_BUBBLE_END_1);
					Parameter parameter2 = grid.GetGridType().get_Parameter(BuiltInParameter.GRID_BUBBLE_END_2);
					ModelCurve modelCurve = this.CreateCurve(grid.Curve, level.Elevation, this.m_uiApp);
					ElementTransformUtils.MoveElement(this.m_doc, modelCurve.Id, new XYZ(0.0, 0.0, level.Elevation));
					modelCurve.LineStyle = style;
					CmdGridToModelCurve.WriteData(modelCurve, this.m_doc);
					list.Add(modelCurve.Id);
					XYZ xyz = grid.Curve.ComputeDerivatives(1.0, true).get_Basis(0).Normalize();
					XYZ xyz2 = grid.Curve.GetEndPoint(0) + xyz * num;
					XYZ xyz3 = grid.Curve.GetEndPoint(1) + xyz * num;
					if (grid.Curve is Line)
					{
						xyz2 = grid.Curve.GetEndPoint(1) + xyz * num;
						xyz3 = grid.Curve.GetEndPoint(0) - xyz * num;
					}
					if (parameter.AsInteger() == 1)
					{
						Arc cur = this.m_doc.CreatYJKArc(xyz2, num, 0.0, 6.2631853071795867, XYZ.BasisX, XYZ.BasisY);
						ModelCurve modelCurve2 = this.CreateCurve(cur, level.Elevation, this.m_uiApp);
						modelCurve2.LineStyle = style;
                        TextNote textNode = this.m_doc.GetTextNode(view, new XYZ(xyz2.X, xyz2.Y + 0.5, level.Elevation), XYZ.BasisX, XYZ.BasisY, 1.0, (TextAlignFlags)128, grid.Name);
						RevitElementExtension.SetTextNoteWidth(textNode, 1.0);
						textNode.ChangeTypeId(type.Id);
						CmdGridToModelCurve.WriteData(textNode, this.m_doc);
						CmdGridToModelCurve.WriteData(modelCurve2, this.m_doc);
						list.Add(modelCurve2.Id);
						list.Add(textNode.Id);
					}
					if (parameter2.AsInteger() == 1)
					{
						Arc cur2 = this.m_doc.CreatYJKArc(xyz3, num, 0.0, 6.2631853071795867, XYZ.BasisX, XYZ.BasisY);
						ModelCurve modelCurve3 = this.CreateCurve(cur2, level.Elevation, this.m_uiApp);
						modelCurve3.LineStyle = style;
                        TextNote textNode2 = this.m_doc.GetTextNode(view, new XYZ(xyz3.X, xyz3.Y + 0.5, level.Elevation), XYZ.BasisX, XYZ.BasisY, 1.0, (TextAlignFlags)128, grid.Name);
						RevitElementExtension.SetTextNoteWidth(textNode2, 1.0);
						textNode2.ChangeTypeId(type.Id);
						CmdGridToModelCurve.WriteData(textNode2, this.m_doc);
						CmdGridToModelCurve.WriteData(modelCurve3, this.m_doc);
						list.Add(modelCurve3.Id);
						list.Add(textNode2.Id);
					}
					this.HideGrid(list, viewList);
					if (tempUsePBar != null)
					{
						if (tempUsePBar.IsDisposed && tempUsePBar.m_stopTag)
						{
							result = false;
							return result;
						}
						tempUsePBar.RefreshProgressBar();
						if (tempUsePBar.IsDisposed && tempUsePBar.m_stopTag)
						{
							result = false;
							return result;
						}
					}
				}
			}
			return result;
		}

		private void HideGrid(List<ElementId> ic, List<Autodesk.Revit.DB.View> viewList)
		{
			FilteredElementCollector filteredElementCollector = new FilteredElementCollector(this.m_doc);
			filteredElementCollector.OfClass(typeof(Autodesk.Revit.DB.View));
			List<Element> list = filteredElementCollector.ToElements().ToList<Element>();
			new List<Element>();
			for (int i = 0; i < list.Count; i++)
			{
				Autodesk.Revit.DB.View view = list[i] as Autodesk.Revit.DB.View;
				foreach (Autodesk.Revit.DB.View view2 in viewList)
				{
					if (view.Name == view2.Name)
					{
						list.RemoveAt(i);
						break;
					}
				}
			}
			foreach (Element element in list)
			{
				SubTransaction subTransaction = new SubTransaction(this.m_doc);
				subTransaction.Start();
				(element as Autodesk.Revit.DB.View).HideElements(ic);
				subTransaction.Commit();
			}
		}

		private static List<Autodesk.Revit.DB.View> GetAllView(Autodesk.Revit.DB.Document doc)
		{
			List<Autodesk.Revit.DB.View> list = new List<Autodesk.Revit.DB.View>();
			FilteredElementCollector filteredElementCollector = new FilteredElementCollector(doc);
			filteredElementCollector.OfClass(typeof(View3D));
			foreach (Element element in filteredElementCollector.ToElements())
			{
				View3D view3D = element as Autodesk.Revit.DB.View3D;
				if (!view3D.IsTemplate)
				{
					list.Add(view3D);
				}
			}
			return list;
		}

		private static List<GraphicsStyle> GetAllLineStyle(Autodesk.Revit.DB.Document doc)
		{
			List<GraphicsStyle> list = new List<GraphicsStyle>();
			foreach (Element element in new FilteredElementCollector(doc).OfClass(typeof(GraphicsStyle)))
			{
				GraphicsStyle graphicsStyle = element as GraphicsStyle;
				if (graphicsStyle != null)
				{
					Category graphicsStyleCategory = graphicsStyle.GraphicsStyleCategory;
					if (graphicsStyleCategory != null)
					{
						Category parent = graphicsStyleCategory.Parent;
						if (parent != null && (parent.Name.CompareTo("线") == 0 || parent.Name.CompareTo("Lines") == 0) && graphicsStyleCategory.Name.CompareTo(graphicsStyle.Name) == 0)
						{
							int integerValue = graphicsStyleCategory.Id.IntegerValue;
							if (integerValue == -2000078 || integerValue == -2000288 || integerValue == -2000285 || integerValue == -2000284 || integerValue == -2000287 || integerValue == -2000286 || integerValue == -2000043 || integerValue == -2000044 || integerValue == -2000042 || integerValue == -2009504)
							{
								list.Add(graphicsStyle);
							}
						}
					}
				}
			}
			return list;
		}

		private static List<TextNoteType> GetAllTextStyle(Autodesk.Revit.DB.Document doc)
		{
			List<TextNoteType> list = new List<TextNoteType>();
			FilteredElementCollector filteredElementCollector = new FilteredElementCollector(doc);
			filteredElementCollector.OfClass(typeof(TextNoteType));
			foreach (Element element in filteredElementCollector.ToElements())
			{
				TextNoteType item = (TextNoteType)element;
				try
				{
					list.Add(item);
				}
				catch
				{
				}
			}
			return list;
		}

		private static List<Level> GetAllLevel(Autodesk.Revit.DB.Document doc)
		{
			List<Level> list = new List<Level>();
			FilteredElementCollector filteredElementCollector = new FilteredElementCollector(doc);
			filteredElementCollector.OfClass(typeof(Level));
			foreach (Element element in filteredElementCollector.ToElements())
			{
				Level item = (Level)element;
				try
				{
					list.Add(item);
				}
				catch
				{
				}
			}
			return list;
		}

		private static Schema GetSchema(Autodesk.Revit.DB.Document doc)
		{
			Schema schema = Schema.Lookup(CmdGridToModelCurve.DataGuid);
			if (schema == null)
			{
				SubTransaction subTransaction = new SubTransaction(doc);
				try
				{
					subTransaction.Start();
					SchemaBuilder schemaBuilder = new SchemaBuilder(CmdGridToModelCurve.DataGuid);
					schemaBuilder.SetSchemaName("DataSchema");
					schemaBuilder.AddSimpleField("HYData", typeof(string));
					schemaBuilder.Finish();
					subTransaction.Commit();
				}
				catch
				{
					subTransaction.RollBack();
				}
			}
			return schema;
		}

		public static string WriteData(Element elem, Autodesk.Revit.DB.Document doc)
		{
			string result = "";
			SubTransaction subTransaction = new SubTransaction(doc);
			try
			{
				subTransaction.Start();
				Schema schema = CmdGridToModelCurve.GetSchema(doc);
				Entity entity = new Entity(schema);
				Field field = schema.GetField("HYData");
				entity.Set<string>(field, "Grid");
				elem.SetEntity(entity);
				subTransaction.Commit();
			}
			catch
			{
				subTransaction.RollBack();
			}
			return result;
		}

		public static string ReadData(Element elem)
		{
			string result;
			try
			{
				Entity entity = elem.GetEntity(Schema.Lookup(CmdGridToModelCurve.DataGuid));
				if (entity == null)
				{
					result = "";
				}
				else
				{
					result = entity.Get<string>(Schema.Lookup(CmdGridToModelCurve.DataGuid).GetField("HYData"));
				}
			}
			catch
			{
				result = "";
			}
			return result;
		}

		public static Guid DataGuid = new Guid("356cda5a-e6c5-4c2f-a9ef-b1223336b8c8");
	}
}
